package com.starblink.store.memanage.adapter

import android.graphics.Color
import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.util.FontUtil
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.store.databinding.ItemMemanageStoreBinding
import com.starblink.store.databinding.ItemMineManageEmptyBinding
import com.starblink.store.databinding.ItemMineManageTitleBinding
import kotlinx.coroutines.MainScope

class SubStoresCell(
    binding: ItemMemanageStoreBinding,
    val detailListener: (Int, MerchantWebVOF) -> Unit,
    val unFollowListener: (Int, MerchantWebVOF) -> Unit,
) :
    BaseBindingVH<MerchantWebVOF, ItemMemanageStoreBinding>(binding = binding) {

    override fun onBind(merchat: MerchantWebVOF, position: Int) {
        binding.manage.cont.trackData(
            page = SpmPageDef.MeStoresSubListPage,
            element = SpmElementDef.ELE_33032,
            params = mapOf(
                "merchantName" to "${merchat.merchantName}",
                "index" to position.toString()
            ),
            businessSuffixID = businessSuffixId(merchat)
        )
        binding.deleteBtn.click {
            CommonMutateManage.subscribeOrNotMerchant(
                itemView.context,
                merchat.id,
                merchat.merchantName,
                merchat.subOrNot,
                MainScope(),
                cancelNotCallback = {
                    binding.laySidesLipDelete.quickClose()
                },
                resultCallback = {
                    if (it) {
                        binding.laySidesLipDelete.quickClose()
                        unFollowListener.invoke(position, merchat)
                    }
                }, merchant = merchat
            )
            SpmTrackHandler.addSingleTrackData(
                SkAntEntity(
                    event = TrackEvent.elementClick.value,
                    spmCnt = GTrackerAssistUtils.fetchElementValue(
                        SpmPageDef.MeStoresSubListPage,
                        SpmElementDef.StoreSub
                    ),
                    params = mapOf(
                        "merchantName" to (merchat.merchantName ?: ""),
                        "index" to position.toString(),
                        "result" to "0"
                    )
                )
            )
        }

        binding.manage.cont.click {
            detailListener.invoke(position, merchat)
        }
        binding.manage.tagRed.visibleOrGone(merchat.read == false) {}

        binding.manage.tags.sale.visibleOrGone(merchat.isOnSale == true) {}
        binding.manage.title.text = merchat.merchantName
        binding.manage.ivStore.load(merchat.logo ?: "", merchat.merchantName)
        binding.manage.tags.tvFollowers.text =
            "${merchat.subNum} ${CommUtils.getString(com.starblink.basic.style.R.string.following_2)}"
        binding.manage.tags.divShop.visibleOrGone(merchat.merchantDiscountInfo != null) {}
        binding.manage.tags.ivShop.visibleOrGone(merchat.merchantDiscountInfo != null) {
            var info: MerchantWebVOF.MerchantDiscountInfo? = merchat.merchantDiscountInfo
            buildSpannableString {
                if (!info?.prefix.isNullOrEmpty()) {
                    addText("${info?.prefix} ") {
                        setColor(Color.parseColor("#F55858"))
                        FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                            setTypeface(it)
                        }
                    }
                }
                addText(info?.discount ?: "") {
                    setColor(Color.parseColor("#F55858"))
                    setScale(1.2f)
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_BOLD)?.let {
                        setTypeface(it)
                    }
                }
                if (!info?.suffix.isNullOrEmpty()) {
                    addText(" ${info?.suffix}") {
                        setColor(Color.parseColor("#F55858"))
                        FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                            setTypeface(it)
                        }
                    }
                }
            }
        }
    }
}

/**
 * 大标题
 */
class ManageTitleCell(
    binding: ItemMineManageTitleBinding
) :
    BaseBindingVH<String, ItemMineManageTitleBinding>(binding = binding) {

    override fun onBind(tip: String, position: Int) {
        binding.clTitleTxt.text = tip
    }

}

//空布局暂未
class ManageStoreEmptyCell(
    binding: ItemMineManageEmptyBinding
) :
    BaseBindingVH<Int, ItemMineManageEmptyBinding>(binding = binding) {

    override fun onBind(type: Int, position: Int) {
        binding.tv.text =
            if (type == 0) "Follow your favorite stores to keep updated with the latest store discounts." else "Follow your favorite brands to keep updated with the latest brand discounts."
    }

}